"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ustore-security
Case Name   : 给普通用户赋予列的所有操作权限
Create At   : 2022/5/19
Owner       : @zou_jialiang050
Description :
    1.创建普通用户
    2.系统管理员创建表
    3.给用户赋予列的所有操作权限
    4.用户插入数据
    5.用户查询数据
    6.用户更新数据
Expect      :
    1.用户创建成功
    2.创建表成功
    3.赋予权限成功
    4.数据插入成功
    5.数据查询成功
    6.数据更新成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Ustore(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.userNode = Node(node='PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = 'user_ustore_security_public_0001'
        self.table = 't_ustore_security_public_0001'
    
    def test_security(self):
        text = '-----step1：创建普通用户；expect：成功-----'
        self.logger.info(text)
        sql_cmd1 = f'drop user if exists {self.pri_user};' \
            f'create user {self.pri_user} with independent identified' \
            f' by \'{macro.COMMON_PASSWD}\';'
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(msg1)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, msg1,
                      '执行失败:' + text)
        
        text = '-----step2：系统管理员创建表；expect：成功-----'
        self.logger.info(text)
        sql_cmd2 = f"drop table if exists {self.table};" \
            f"create table {self.table}(id int," \
            f"identify_id char(16) not null,name varchar(20)) with " \
            f"(storage_type=ustore);" \
            f"insert into {self.table} values(1, '621542321445987', 'lili');"
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2)
        self.logger.info(msg2)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg2,
                      '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg2,
                      '执行失败:' + text)
        
        text = '-----step3：给用户赋予列的所有操作权限；expect：成功-----'
        self.logger.info(text)
        sql_cmd3 = f'grant all privileges (id, identify_id) on {self.table} ' \
            f'to {self.pri_user};'
        msg3 = self.sh_primy.execut_db_sql(sql_cmd3)
        self.logger.info(msg3)
        self.assertIn(self.constant.GRANT_SUCCESS_MSG, msg3, '执行失败:' + text)
        
        text = '-----step4：用户插入数据；expect：成功-----'
        self.logger.info(text)
        content = f'-U {self.pri_user} -W {macro.COMMON_PASSWD}'
        sql_cmd4 = f"insert into {self.table}(id, identify_id) " \
            f"values (2, '325647981203251');"
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4, sql_type=f'{content}')
        self.logger.info(msg4)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg4,
                      '执行失败:' + text)
        
        text = '-----step5：用户查询数据；expect：成功-----'
        self.logger.info(text)
        sql_cmd5 = f'select identify_id from {self.table};'
        content = f'-U {self.pri_user} -W {macro.COMMON_PASSWD}'
        msg5 = self.sh_primy.execut_db_sql(sql_cmd5, sql_type=f'{content}')
        self.logger.info(msg5)
        self.assertIn('621542321445987', msg5, '执行失败:' + text)
        
        text = '-----step6：用户更新数据；expect：成功-----'
        self.logger.info(text)
        sql_cmd6 = f'update {self.table} set identify_id=\'425645231201253\'' \
            f' where id=1;'
        content = f'-U {self.pri_user} -W {macro.COMMON_PASSWD}'
        msg6 = self.sh_primy.execut_db_sql(sql_cmd6, sql_type=f'{content}')
        self.logger.info(msg6)
        self.assertIn(self.constant.UPDATE_SUCCESS_MSG, msg6,
                      '执行失败:' + text)
    
    def tearDown(self):
        text = '-----清理用户、表-----'
        self.logger.info(text)
        sql_cmd1 = f'drop table {self.table} cascade;' \
            f'drop user {self.pri_user} cascade;'
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1)
        self.logger.info(msg1)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, msg1,
                      '执行失败:' + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, msg1,
                      '执行失败:' + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
